package com.hbnu.net;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author kittychen
 * @since 2025-10-28 19:31
 */
public class TcpClientDemo01 {

    public static void main(String[] args) {
        Socket socket = null;
        try {
            // 1、创建Socket对象，指明服务器端的ip和端口号
            socket = new Socket("127.0.0.1", 8888);

            // 2、获取输出流
            OutputStream outputStream = socket.getOutputStream();

            // 3、发送数据
            outputStream.write("hello,server".getBytes());
            System.out.println("数据发送成功！！！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 4、释放资源
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
